package dryven.util.json.dryvenjson.encoders;

import java.io.IOException;
import java.util.Date;

import dryven.model.di.LocalThreadStorage;
import dryven.util.json.dryvenjson.FieldPolicy;
import dryven.util.json.dryvenjson.JSONCompositeEncoder;
import dryven.util.json.dryvenjson.JSONTypeEncoder;

public class DateEncoder implements JSONTypeEncoder {

	@Override
	public boolean isApplicableForType(Class<?> type) {
		return Date.class.isAssignableFrom(type);
	}

	@Override
	public void encodeValue(Object value, JSONCompositeEncoder e,
			FieldPolicy p, LocalThreadStorage diStorage, Appendable dest)
			throws IOException {
		Date date = (Date) value;
		dest.append(Long.toString((date.getTime()/1000)));
	}
}
